package com.helloandroid.view

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import com.github.mikephil.charting.formatter.IFillFormatter
import com.helloandroid.AppUtil
import com.helloandroid.R
import com.helloandroid.tools.MyLog
import dai.common.util.CommonUtils
import kotlin.math.min

class CircleView
@JvmOverloads constructor(
        ctx: Context,
        attrs: AttributeSet? = null,
        defStyle: Int = 0
) : View(ctx, attrs, defStyle) {

    val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    val r = ctx.resources.getDimension(R.dimen.dp80)
    val bgColor = ctx.resources.getColor(R.color.circle_view_bg,null)
    val fgColor = ctx.resources.getColor(R.color.dd_main_blue,null)

    // 是否正在动画
    var bAniming = false
    var iMax = DefaultTarget  // 当天的目标步数
    var iDst = 0    // 当天走的步数
    var iCur = 0 //   用于动画的步数

    companion object{
        const val DefaultTarget = 6000
    }

    init {
        paint.strokeWidth = ctx.resources.getDimension(R.dimen.dp8)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)

        var newWidth = widthSize
        var newHeight = heightSize

        // width
        if (widthMode == MeasureSpec.EXACTLY) {
            newWidth = widthSize
        } else {
            newWidth = widthSize
            if (widthMode == MeasureSpec.AT_MOST) {
                newWidth = min(newWidth, AppUtil.dm.widthPixels)
            }
        }

        // height
        if (heightMode == MeasureSpec.EXACTLY) {
            newHeight = heightSize
        } else {
            newHeight = heightSize
            if (heightMode == MeasureSpec.AT_MOST) {
                newHeight = min(newHeight, resources.getDimension(R.dimen.dp220).toInt())
            }
        }
        setMeasuredDimension(newWidth, newHeight)
    }

    override fun onDraw(pcanvas: Canvas?) {
        super.onDraw(pcanvas)

        val canvas = pcanvas!!

        canvas.save()

        // 绘制外面的圆
        canvas.translate(width/2f,height/2f)

        paint.style = Paint.Style.STROKE

        // 绘制背景
        paint.color = bgColor
        canvas.drawArc(RectF(-r,-r,r,r)
            ,0f,360f,false,paint)

        // 计算角度
        val percent = CommonUtils.clamp01(iCur.toFloat()/ iMax)
        val degress = percent * 360f

        // 绘制前景
        paint.color = fgColor
        canvas.drawArc(RectF(-r,-r,r,r)
                ,180f,degress,false,paint)

        canvas.restore()
    }

    fun animTo(bs:Int,bsTarget:Int){

        if (bAniming){
            stopAnim()
        }

        iCur = 0
        iMax = bsTarget
        iDst = bs

        // 1s
        if (bs > 0){
            bAniming = true
            valueAnimator = ValueAnimator.ofInt(0,iDst)
            valueAnimator!!.duration = 1000
            valueAnimator!!.addUpdateListener(updateListener)
            valueAnimator!!.start()
        }else{
            invalidate()
        }
    }

    private fun stopAnim(){
        valueAnimator?.removeAllUpdateListeners()
        valueAnimator = null
    }

    var valueAnimator:ValueAnimator? = null
    private val updateListener = ValueAnimator.AnimatorUpdateListener { valueAnim->
        val percent = valueAnim.animatedFraction
        iCur = (percent * iDst).toInt()

        if (percent >= 1.0f){
            iCur = iDst
            valueAnim.removeAllUpdateListeners()
        }
        invalidate()
    }
}